<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ExportWithTableName;
use App\Baby;
use App\Course;
use App\Town;
use App\CourseSubscribe;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Facades\DB;
use App\Admin\Extensions\CourseSubExpoter;


class SubscribeController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function destroy($id)
    {
        $subscribe = CourseSubscribe::find($id);
        $course_time =  $subscribe->course_time()->first();
        if(!empty($course_time)){
            $course_time->people_number = $course_time->people_number  -1;
            $course_time->save();
        }

        if ($this->form()->destroy($id)) {
            return response()->json([
                'status'  => true,
                'message' => trans('admin.delete_succeeded'),
            ]);
        } else {
            return response()->json([
                'status'  => false,
                'message' => trans('admin.delete_failed'),
            ]);
        }
    }

    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('预约管理');
            $content->description('预约列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('预约管理');
            $content->description('编辑预约信息');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('预约管理');
            $content->description('新增预约信息');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(CourseSubscribe::class, function (Grid $grid) {
            $grid->disableCreation();

            $grid->actions(function ($actions){
                $actions->disableEdit();
            });
            $towns = DB::table('towns')->select()->get()->all();
            $towns = array_combine( array_column($towns,'id'),  array_column($towns,'name') );
            $month_ages = DB::select('select id, title as name  from month_ages');
            $month_ages = array_combine(array_column($month_ages, 'id'), array_column($month_ages, 'name'));
            $grid->exporter(new ExportWithTableName('baby_course_subscribe',
                'course_subscribe'.date('Y-m-d', time()),
                ['name', 'birth','sex','guardian','tel','habitual_residence', 'town_name','course_name', 'course_time','month_age'],
                ['sex'=>['0'=>'女','1'=> '男']],
                ['宝宝名字', '出生日期', '性别','家长姓名','联系电话','户籍所在地','所属村居','课程名','上课时间','月龄段']
            ));
            $grid->filter(function($filter) use($month_ages, $towns){
                $filter->disableIdFilter();
                $filter->like('baby.name','宝宝名');
                $filter->between('baby.birth', '出生日期')->date();
                $filter->between('course.course_time', '上课时间')->date();
                $filter->like('baby.guardian','家长姓名');
                $filter->like('course.name','课程名');
                $filter->equal('course.month_age_id', '月龄段')->select($month_ages);
                $filter->equal('baby.town_id', '所属村居')->select($towns);
            });

            $grid->column('baby.name', '宝宝名字');
            $grid->column('baby.birth', '出生日期');
            $grid->column('baby.sex', '性别')->display(function($sex){
                return $sex? '男': '女';
            });
            $grid->column('baby.guardian', '家长姓名');
            $grid->column('baby.tel','联系电话');
            $grid->column('baby.habitual_residence', '户籍所在地');
            $grid->column('baby.town_id','所属村居')->display(function($town_id) use ($towns){
                return isset($towns[$town_id]) ? $towns[$town_id]: ''; 
            });
            $grid->column('course.name', '课程名');
            $grid->column('course_time.course_time', '课程时间');
            $grid->column('course.month_age_id', '月龄段')->display(function($month_age_id) use ($month_ages) {
               return isset($month_ages[$month_age_id]) ? $month_ages[$month_age_id]: '';  
            } );
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(CourseSubscribe::class, function (Form $form) {
            $babies =  DB::select('select id, name  from baby');
            $babies =  array_combine(array_column($babies,'id'),array_column($babies,'name'));

            $courses =  DB::select('select id, name from courses');
            $courses =  array_combine(array_column($courses,'id'),array_column($courses,'name'));


            // $form->display('id', 'ID');

            $form->select('baby_id', '宝宝')->options($babies);
            $form->select('course_id', '课程')->options($courses);

            $form->display('created_at', 'Created At');
        });
    }
}
